package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main5Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        //6.o 动态授权

        //文件流
//        new FileInputStream()
//        new FileOutputStream(路径path)

        try {
            FileOutputStream fileOutputStream = openFileOutput("aaa.txt", MODE_PRIVATE);

            fileOutputStream.write("呵呵呵呵".getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileInputStream fileInputStream = openFileInput("aaa.txt");
            byte[] bytes = new byte[1024];
            int len = 0;
            String s = null;
            while((len = fileInputStream.read(bytes)) != -1){
                 s = new String(bytes, 0, len);
            }

            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 110 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "同意", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "不同意", Toast.LENGTH_SHORT).show();
        }
    }

    public void write(View view) {

        //只需一次.全aap都能用
        //动态 申请.
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},110);
        File file = Environment.getExternalStorageDirectory();
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File(file, "a.txt"));
            fileOutputStream.write("呵呵呵呵".getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void read(View view) {
        File file = Environment.getExternalStorageDirectory();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(file, "a.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[1024];
        int len = 0;
        String s = null;
        while(true){
            try {
                if (!((len = fileInputStream.read(bytes)) != -1)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            s = new String(bytes, 0, len);
        }

        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

    }
}
